<template>
  <div class='container'>
    <el-card>
      <div class="left">
        <div>
          <span>关键字</span>
        <el-input v-model="input" style="width:70%" placeholder="根据编号搜索"></el-input>
        </div>
        <el-row>
          <el-button size="small" @click="clear">清除</el-button>
          <el-button size="small" type="primary" @click="search()">搜索</el-button>
        </el-row>
      </div>
      <el-alert
          :title="alertText"
          type="info"
          show-icon
          :closable="false"
        />
        <!-- 表格 -->
        <el-table
          style="width: 100%" :data="items">
          <el-table-column
          prop="id"
            label="编号"
            width="180">
          </el-table-column>
          <el-table-column

          prop="questionType"
          :formatter="formatterFn"
            label="题型"
            width="100">
            <!-- {{formatter}} -->
          </el-table-column>
          <el-table-column
          prop="questionIDs"
            label="题目编号"
            width="280"
            >
            <template #default="{row}">
              <!-- {{row}} -->
              <!-- questionIDs -->
              <!-- <span>{{row.questionIDs}}</span> -->
              <div @click="handleSubject(row.questionIDs)">
                <div class="handBtn" style="color:#81a1f7" v-for="(item,index) in row.questionIDs" :key="index">{{item.number}}
              </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
          prop="addTime"
            label="录入时间" width="240">
          </el-table-column>
          <el-table-column
          prop="totalSeconds"
            label="答题时间(s)" width="240">
          </el-table-column>
          <el-table-column
          prop="accuracyRate"
            label="正确率(%)" width="200">
          </el-table-column>
          <el-table-column
          prop="userName"
            label="录入人">
          </el-table-column>
          <el-table-column
            label="操作">
            <template #default="{row}">

              <div class="del" @click="del(row.id)">
                <i class="el-icon-delete"></i>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div class="page">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="requestParameters.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="requestParameters.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts">
      </el-pagination>
        </div>
    </el-card>
    <questions-preview
    :showDialog="showDialog"
    @close="closeDialogFn"
    :data="data"
    ></questions-preview>
  </div>
</template>

<script>
//
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import questionsPreview from '../components/questions-preview.vue'
import { questionType } from '@/api/hmmm/constants'
export default {
  components: { questionsPreview },
  data () {
    return {
      questionType: questionType,
      showDialog: false,
      input: '',
      requestParameters: {
        page: 1,
        pagesize: 20
      },
      items: [],
      counts: 0,
      alertText: '',
      data: {}
    }
  },
  created () {
    this.getRandoms()
  },
  methods: {

    // 搜索
    search () {
      this.requestParameters.pagesize = 1
      this.getRandoms()
    },
    // 题型渲染
    formatterFn (row, column, cellValue, index) {
      return questionType.find(item => item.value === +cellValue)?.label || '未知'
    },

    // 关闭弹窗
    closeDialogFn () {
      this.showDialog = false
    },
    // 打开弹窗
    async  handleSubject (row) {
      this.showDialog = true
      const [obj] = row
      // console.log(obj)
      const { data } = await detail(obj)
      console.log(data)
      this.data = data
    },
    // 清空搜索框
    clear () {
      this.input = ''
    },
    // 删除
    del (user) {
      this.$confirm('确认删除该文件?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms({ id: user })
        if (this.items.length === 1 && this.requestParameters.page > 1) {
          this.requestParameters.page--
        }
        // console.log(data)
        this.items.splice(user, 1)
        this.$message.success('删除成功')
        this.getRandoms(this.requestParameters)
      }).catch(() => {
        this.$message.error('取消删除')
      })
    },
    // 获取列表
    async getRandoms () {
      const { data: { items, counts } } = await randoms(this.requestParameters)
      // console.log(items)
      this.items = items
      this.counts = counts
      this.alertText = `共 ${this.counts} 条记录`
    },
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      this.getRandoms()
    },
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.getRandoms()
    }
  }
}
</script>

<style scoped lang='less'>
.container{
  padding: 10px;
}
  span {
    font-size: 14px;
    font-weight: 700;
    margin-right: 10px;
  }
  .left{
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
  .del{
    width: 40px;
    height: 40px;
    background-color: #f3cdcd;
    border-radius: 50%;
    &:hover{
      background-color: #e17570;
      .el-icon-delete{
        color: #fff;
      }
    }
    .el-icon-delete{
      line-height: 40px;
      margin-left: 14px;
      color: #e57585;
    }
  }
  .page{
    margin-top: 15px;
    display: flex;
      justify-content: flex-end;
  }
  .handBtn{
    cursor: pointer;
  }
  ::v-deep{
    .el-alert{
      margin-bottom: 15px;
    }

  }
</style>
